import Vue from 'vue'
import axios from 'axios'
import power from './power.sdk.js'
import {SER_URL} from "./warehouse.js"



// create an axios instance
const service = axios.create({
	baseURL: SER_URL, // url = base url + request url
	// withCredentials: true, // send cookies when cross-domain requests 注意：withCredentials和后端配置的cross跨域不可同时使用
	timeout: 15000, // request timeout
	// timeout: 40000,
	crossDomain: true
})

// request拦截器,在请求之前做一些处理
service.interceptors.request.use(
	config => {
		const token = sessionStorage.getItem(power.ACCESS_TOKEN)
		// 如果 token 存在
		// 让每个请求携带自定义 token 请根据实际情况自行修改
		if (token) {
			config.headers[power.AUTHORIZATION] = 'Bearer ' + token
		}
		return config
	},
	error => {
		if (error.response) {
			const data = error.response.data
			uni.showToast({
				icon: 'none',
				message: data.msg,
				duration: 3000,
			})
		} else {
			const err = error + ''
			if (err.indexOf('timeout') !== -1) {
				uni.showToast({
					icon: 'none',
					message: '网络繁忙，请稍后重试',
					duration: 3000
				})
			} else if (err.indexOf('Network Error') !== -1) {
				uni.showToast({
					icon: 'none',
					message: '服务异常',
					duration: 3000
				})
			}
		}
		return Promise.reject(error);
	}
);

//配置成功后的拦截器
service.interceptors.response.use(res => {
	let headers = res.headers;
	// 如果后端响应了token则更新token
	if (headers.authorization) {
		sessionStorage.setItem(power.ACCESS_TOKEN, headers.authorization);
	}
	// console.log('拦截器', res.data)
	if (res.status == 200) {
		if (res.data.code === 0) {
			return res.data;
		} else {
			console.log("请求错误信息", res.data.msg);
			uni.showToast({
				icon: 'error',
				title: res.data.msg,
				duration: 3000,
				position: 'top'
			})
			return Promise.reject(res.data.msg);
		}
	} else {
		console.log("请求错误信息", res.data.msg);
		uni.showToast({
			icon: 'error',
			title: res.data.msg,
			duration: 3000
		})
		return Promise.reject(res.data.msg);
	}
}, error => {
	// 全局异常处理
	console.log('error------------', error);
	if (error.response.status) {
		switch (error.response.status) {
			case 400:
				console.log("code: 400 错误请求");
				break;
			case 401:
				console.log("code: 401 错误请求");
				break;
			case 403:
				console.log("code: 403 拒绝访问");
				break;
			case 404:
				console.log("code: 404 请求错误，未找到该资源");
				break;
			case 405:
				console.log("code: 405 请求方法未允许");
				break;
			case 408:
				console.log("code: 408 请求超时");
				break;
			case 500:
				console.log("code: 500 服务器端出错");
				break;
			case 501:
				console.log("code: 501 网络未实现");
				break;
			case 502:
				console.log("code: 502 网络错误");
				break;
			case 503:
				console.log("code: 503 服务不可用");
				break;
			case 504:
				console.log("code: 504 网络超时");
				break;
			case 505:
				console.log("code: 505 http版本不支持该请求");
				break;
			default:
				console.log(error);
				break;
		}
	} else {
		alert(error.message)
	}
	return Promise.reject(error)
})

// 在disposal_content中放入这段自定义适配器的代码，就可以实现uniapp的app和小程序开发中能使用axios进行跨域网络请求，并支持携带cookie
axios.defaults.adapter = function(config) {
	return new Promise((resolve, reject) => {
		var settle = require('axios/lib/core/settle');
		var buildURL = require('axios/lib/helpers/buildURL');
		uni.request({
			method: config.method.toUpperCase(),
			url: config.baseURL + buildURL(config.url, config.params, config.paramsSerializer),
			header: config.headers,
			data: config.data,
			dataType: config.dataType,
			responseType: config.responseType,
			sslVerify: config.sslVerify,
			complete: function complete(response) {
				response = {
					data: response.data,
					status: response.statusCode,
					errMsg: response.errMsg,
					header: response.header,
					config: config
				};
				settle(resolve, reject, response);
			}
		})
	})
}

export default service
